package edu.cicese.alz.swing;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 7/06/12
 * Time: 03:02 PM
 */
public class ColoredToggleButton extends JButton {
	private boolean selected = false;
	private JPopupMenu popUpMenu;
	private Color backgroundColor;

	private JPanel panel;
	private String panelName;

	public ColoredToggleButton(boolean selected, Color backgroundColor) {
		super();
		setSelected(selected);
		this.backgroundColor = backgroundColor;
	}

	public ColoredToggleButton(boolean selected) {
		this(selected, SwingUtil.cBackground);
	}

	public void setPopUpMenu(JPopupMenu popUpMenu) {
		this.popUpMenu = popUpMenu;
	}

	public void addListener() {
		final ColoredToggleButton cb = this;
		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent evt) {
				if (!isSelected() && isEnabled()) {
					setBackground(SwingUtil.cRollover);
					setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));
				}
			}

			public void mouseExited(MouseEvent evt) {
				if (!isSelected()) {
					deselect();
				}
			}

			public void mousePressed(MouseEvent evt) {
				if (isEnabled()) {
					setBorder(BorderFactory.createLineBorder(SwingUtil.cPressedBorder));
				}
			}

			public void mouseClicked(MouseEvent evt) {
				if (isEnabled()) {
					if (isSelected()) {
						deselect();
					}
					else {
						select();
						if (popUpMenu != null) {
							popUpMenu.show(cb, 0, cb.getHeight());
						}
					}
//					setSelected(!isSelected());
//					selected = !selected;
//					if (isSelected()) {
//
//						setBackground(SwingUtil.cPressed);
//						setBorder(BorderFactory.createLineBorder(SwingUtil.cPressedBorder));
//					} else {
//						setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));
//						setBackground(SwingUtil.cRollover);
//					}
				}
			}
		});
	}

	public void setCardLayout(JPanel panel, String panelName) {
		this.panel = panel;
		this.panelName = panelName;
	}

	public void addTypeListener(final ColoredToggleButton[] cbs) {
		final ColoredToggleButton cb = this;

		addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent evt) {
				if (!isSelected() && isEnabled()) {
					setBackground(SwingUtil.cRollover);
					setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));
				}
			}

			public void mouseExited(MouseEvent evt) {
				if (!isSelected()) {
					deselect();
				}
			}

			public void mousePressed(MouseEvent evt) {
				if (isEnabled() && !isSelected()) {
					select();
					if (isSelected()) {
						if (popUpMenu != null) {
							popUpMenu.show(cb, 0, cb.getHeight());
						}
						for (ColoredToggleButton button : cbs) {
							button.deselect();
						}
					} else {
//						setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));
//						setBackground(SwingUtil.cRollover);
					}
				}
				/*if (isEnabled()) {
					setBorder(BorderFactory.createLineBorder(SwingUtil.cPressedBorder));
				}*/
			}

			public void mouseClicked(MouseEvent evt) {
				/*if (isEnabled()) {
					select();
					if (isSelected()) {
						if (popUpMenu != null) {
							popUpMenu.show(cb, 0, cb.getHeight());
						}
						for (ColoredToggleButton button : cbs) {
							button.deselect();
						}
					} else {
//						setBorder(BorderFactory.createLineBorder(SwingUtil.cRolloverBorder));
//						setBackground(SwingUtil.cRollover);
					}
				}*/
			}
		});
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public void select() {
		setSelected(true);
		setBackground(SwingUtil.cPressed);
		setBorder(BorderFactory.createLineBorder(SwingUtil.cPressedBorder));

		if (panel != null) {
			((CardLayout) panel.getLayout()).show(panel, panelName);
		}
	}

	public void deselect() {
		setSelected(false);
		setBackground(backgroundColor);
		setBorder(new EmptyBorder(1, 1, 1, 1));
	}
}
